package com.ubing.boot.notesforjava.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName : downLoadPool
 * @Description : downLoadPool
 * @Author : Xxxxx
 * @Date: 2023-03-20 14:30
 */
@Component
public class downLoadPool {
        // 阿里规约-不允许使用Executors.newFixedThreadPool创建线程池，该方式对线程控制粒度低
        //  ExecutorService executorService = Executors.newFixedThreadPool(1);
        //  推荐手动创建线程池，方便控制
        //  ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue，ThreadFactory threadFactory,RejectedExecutionHandler handler)
        /***
        * 7个参数
         * int corePoolSize,
         * int maximumPoolSize,
         * long keepAliveTime,
         * TimeUnit unit,
         * BlockingQueue<Runnable> workQueue，
         * ThreadFactory threadFactory,
         * RejectedExecutionHandler handler
        */

     private static ThreadFactory nameThreadFactory= new ThreadFactory() {
           int i=0;
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"线程"+i++);
            }
        };


    @Bean
    public ThreadPoolExecutor executor() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                10,
                60,
                TimeUnit.MICROSECONDS,
                new ArrayBlockingQueue<>(5),
                nameThreadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

//    @PostConstruct
//    void init(){
//        new Thread(()->{
//            while (true){
//                System.out.println("4123123");
//                try {
//                    Thread.sleep(1000);
//                    System.out.println("over");
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//        }).start();
//    }

    public static void main(String[] args) {
        List<String> originalList = new ArrayList<>();
        for (int i = 0; i < 950; i++) {
            originalList.add(String.valueOf(i)+"ss");
        }
        // 假设originalList有1000个元素

        int batchSize = 100; // 拆分的批次大小

        List<List<String>> splitLists = IntStream.range(0, originalList.size())
                .boxed()
                .collect(Collectors.groupingBy(index -> index / batchSize))
                .values()
                .stream()
                .map(indices -> indices.stream().map(originalList::get).collect(Collectors.toList()))
                .collect(Collectors.toList());

        // 打印拆分后的子List
        for (List<String> list : splitLists) {
            System.out.println(list);
        }
    }
}
